package com.bleeth.fx.view.demo5;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import com.bleeth.fx.component.MLabel;
import com.bleeth.fx.component.MSvg;
import com.bleeth.fx.helper.SStyleHelper;
import com.bleeth.fx.layout.FormBuilder;
import com.bleeth.fx.layout.GirdCell;
import com.bleeth.fx.meta.IForm;
import com.bleeth.fx.meta.View;
import javafx.beans.property.Property;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

import java.util.List;

/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@View(value = "left")
public class LeftView implements IForm {


    @Override
    public Region make(Property... properties) {
        JSONObject o1 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "热门推荐");
        JSONObject o2 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "动漫精选");
        JSONObject o3 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "人物精选");
        List<JSONObject> onlineList = CollUtil.newArrayList(o1, o2, o3);

        JSONObject m1 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "我的收藏");
        JSONObject m2 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "下载中心");
        List<JSONObject> myList = CollUtil.newArrayList(m1, m2);

        JSONObject more1 = new JSONObject()
                .set("icon", "icon-remove")
                .set("name", "关于");
        List<JSONObject> moreList = CollUtil.newArrayList(more1);


        VBox view = FormBuilder.form(250D, null)
                .addEmptyRow(20)
                .addRow(GirdCell.createCell(50D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER), GirdCell.createCell(50D, Pos.CENTER))
                .addCell(1, MSvg.builder().path("demo5/logo.svg").height(100).width(100).build().make())
                .endRow(false)

                .addRow(GirdCell.createCell(50D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER), GirdCell.createCell(50D, Pos.CENTER))
                .addCell(1, MLabel.builder().text("One Wallhaven").style((node) -> logoTextStyle(node)).build().make())
                .endRow(false)
                .addEmptyRow(20)

                .addRow(GirdCell.createCell(50D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER), GirdCell.createCell(50D, Pos.CENTER))
                .coverCell(1, menu("在线壁纸", onlineList))
                .endRow(false)

                .addRow(GirdCell.createCell(50D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER), GirdCell.createCell(50D, Pos.CENTER))
                .coverCell(1, menu("我的壁纸", myList))
                .endRow(false)

                .addRow(GirdCell.createCell(50D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER), GirdCell.createCell(50D, Pos.CENTER))
                .coverCell(1, menu("更多", moreList))
                .endRow(false)
                .build();

        return view;
    }


    public Region menu(String title, List<JSONObject> itemList) {
        FormBuilder formBuilder = FormBuilder.form()
                .addRow(GirdCell.createCell(1, Pos.CENTER_LEFT))
                .addCell(0, MLabel.builder().text(title).style((node) -> parentItemStyle(node)).build().make())
                .endRow(false)
                .addEmptyRow(20D);
        for (int i = 0; i < itemList.size(); i++) {
            JSONObject obj = itemList.get(i);
            String icon = obj.getStr("icon");
            String text = obj.getStr("name");
            Label label = MLabel.builder()
                    .icon(icon).height(40)
                    .width(150).text(text)
                    .style((node) -> setItemStyle(node))
                    .action((node -> setItemAction(node)))
                    .build().make();
            formBuilder.addRow(GirdCell.createCell(25D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER_LEFT), GirdCell.createCell(25D, Pos.CENTER))
                    .addCell(1, label)
                            .endRow(false)
                            .addEmptyRow(20D);
        }
        return formBuilder.build();
    }


    public static void setItemAction(Node node) {
        node.setOnMouseClicked(event -> {
            Label label = (Label) node;
            System.err.println(label.getText());
        });
    }

    public static void setItemStyle(Node node) {
        SStyleHelper.node(node)
                .addStyle("-fx-font-size", "16")
                .addStyle("-fx-text-fill", "#ffffff88")
                .addStyle("-fx-font-weight", "bold")
                .addFocusStyle("-fx-border-width","0 0 4 0")
                .addFocusStyle("-fx-border-color","linear-gradient(to right ,#0286ff,#21e7ff)")
                .apply();
    }

    public static void logoTextStyle(Node node) {
        SStyleHelper.node(node)
                .addStyle("-fx-font-size", "20")
                .addStyle("-fx-text-fill", "#ffffff88")
                .addStyle("-fx-font-weight", "bold")
                .apply();
    }

    public static void parentItemStyle(Node node) {
        SStyleHelper.node(node)
                .addStyle("-fx-font-size", "16")
                .addStyle("-fx-text-fill", "#ffffff88")
                .addStyle("-fx-font-weight", "bold")
                .apply();
    }

}
